/* 
   This program imports the concrete imputation .csv files into SAS,
   merges the survu_id and firmid, and saves the merged files.

   input files:
	concrete_imputes02.csv
	concrete_imputes07.csv

   output files:
	concrete.concrete_imputes02
	concrete.concrete_imputes07
*/

%include "ASMimplibs.sas";


/*** BEGIN MACROS ***/

%MACRO import_imputes(year=);


            data WORK.CONCRETE_IMPUTES&year                      ;
            %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
            infile "concrete_imputes&year..csv" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
               informat number $5. ;
               informat PH best32. ;
               informat CM best32. ;
               informat PW best32. ;
               informat SW best32. ;
               informat WW best32. ;
               informat TVS best32. ;
               informat BEA_CEA_Code $5. ;
               informat CEA_ddensity best32. ;
               informat nonpw best32. ;
               informat tab best32. ;
               informat energy best32. ;
               informat impsetnum best32. ;

               format number $5. ;
               format PH best12. ;
               format CM best12. ;
               format PW best12. ;
               format SW best12. ;
               format WW best12. ;
               format TVS best12. ;
               format BEA_CEA_Code $5. ;
               format CEA_ddensity best12. ;
               format nonpw best12. ;
               format tab best12. ;
               format energy best12. ;
               format impsetnum best12. ;

            input
                        number $
                        PH 
                        CM
                        PW
                        SW
                        WW
                        TVS
                        BEA_CEA_Code $
                        CEA_ddensity 
                        nonpw
                        tab
                        energy
                        impsetnum
            ;
            if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
            run;



data concrete_imputes&year (drop = NUMBER);
 set concrete_imputes&year;
 NUMBER_NUM = input(NUMBER,5.);
run;

PROC DATASETS  LIBRARY=WORK;
 MODIFY concrete_imputes&year;
 rename NUMBER_NUM = NUMBER;
RUN;

PROC SORT DATA=concrete_imputes&year;
 by NUMBER;
RUN;

/** Merge survu_id **/

data concrete&year._ids (keep = NUMBER survu_id firmid wt);
 set concrete.concrete&year._gooddata_ids;
 NUMBER = _N_;
run;

data concrete_imputes&year rejimp&year rejid&year ;
 merge concrete&year._ids (in=inid) concrete_imputes&year (in=inimp); 
by NUMBER;
if inid and inimp then output concrete_imputes&year;
else if inid then output rejimp&year;
else output rejid&year;
run;

data concrete.concrete_imputes&year (drop = impsetnum);
 set concrete_imputes&year ;
 _IMPUTE_= impsetnum ;
run;

%MEND;



/**************************/
/******  BEGIN PROGRAM ****/
/**************************/

%import_imputes(year=02);
%import_imputes(year=07);




